﻿/**
 * $RCSfile: LinkTag.java,v $
 * $Revision: 1.1 $
 * $Date: 2009/06/15 08:23:34 $
 *
 * Copyright (C) 2003 ICSS, Inc. All rights reserved.
 *
 * This software is the proprietary information of ICSS, Inc.
 * Use is subject to license terms.
 */
package net.sapfree.display.tags.html;

import javax.servlet.jsp.JspException;

import net.sapfree.display.tags.core.BaseHandlerTag;
import net.sapfree.display.tags.util.ResponseUtils;

/**
 * <p>
 * Title: 信息发布系统（CMS）
 * </p>
 * <p>
 * Description: Generate a URL-encoded hyperlink to the specified URI.
 * </p>
 * 
 * <p>
 * Copyright: Copyright (c) 2003
 * </p>
 * <p>
 * Company: 中软远东国际
 * </p>
 * 
 * @author Andy
 * @version 1.0
 */
public class LinkTag extends BaseHandlerTag {

	// ----------------------------------------------------- Instance Variables

	/**
	 * 
	 */
	private static final long serialVersionUID = 7458246869906378740L;

	/**
	 * The body content of this tag (if any).
	 */
	protected String text = null;

	// ------------------------------------------------------------- Properties

	/**
	 * The anchor to be added to the end of the generated hyperlink.
	 */
	protected String anchor = null;

	public String getAnchor() {
		return this.anchor;
	}

	public void setAnchor(String anchor) {
		this.anchor = anchor;
	}

	/**
	 * The hyperlink URI.
	 */
	protected String href = null;

	public String getHref() {
		return this.href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	/**
	 * The link name for named links.
	 */
	protected String linkName = null;

	public String getLinkName() {
		return this.linkName;
	}

	public void setLinkName(String linkName) {
		this.linkName = linkName;
	}

	/**
	 * The JSP bean name for query parameters.
	 */
	protected String name = null;

	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * The window target.
	 */
	protected String target = null;

	public String getTarget() {
		return this.target;
	}

	public void setTarget(String target) {
		this.target = target;
	}

	/**
	 * Name of parameter to generate to hold index number
	 */
	protected String indexId = null;

	public String getIndexId() {
		return this.indexId;
	}

	public void setIndexId(String indexId) {
		this.indexId = indexId;
	}

	// --------------------------------------------------------- Public Methods

	/**
	 * Render the beginning of the hyperlink.
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 */
	public int doStartTag() throws JspException {

		// Special case for name anchors
		if (linkName != null) {
			StringBuffer results = new StringBuffer("<a name=\"");
			results.append(linkName);
			results.append("\">");
			ResponseUtils.write(pageContext, results.toString());
			return EVAL_BODY_TAG;
		}

		// Generate the opening anchor element
		StringBuffer results = new StringBuffer("<a href=\"");
		results.append(href);
		results.append("\"");
		if (target != null) {
			results.append(" target=\"");
			results.append(target);
			results.append("\"");
		}
		if (accesskey != null) {
			results.append(" accesskey=\"");
			results.append(accesskey);
			results.append("\"");
		}
		if (tabindex != null) {
			results.append(" tabindex=\"");
			results.append(tabindex);
			results.append("\"");
		}
		results.append(prepareStyles());
		results.append(prepareEventHandlers());
		results.append(">");

		// Print this element to our output writer
		ResponseUtils.write(pageContext, results.toString());

		// Evaluate the body of this tag
		return EVAL_BODY_INCLUDE;

	}

	/**
	 * Save the associated label from the body content.
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 */
	public int doAfterBody() throws JspException {

		if (bodyContent != null) {
			String value = bodyContent.getString().trim();
			if (value.length() > 0) {
				text = value;
			}
		}
		return SKIP_BODY;

	}

	/**
	 * Render the end of the hyperlink.
	 * 
	 * @exception JspException
	 *                if a JSP exception has occurred
	 */
	public int doEndTag() throws JspException {

		// Prepare the textual content and ending element of this hyperlink
		StringBuffer results = new StringBuffer();
		if (text != null) {
			results.append(text);
		}
		results.append("</a>");

		// Render the remainder to the output stream
		ResponseUtils.write(pageContext, results.toString());

		// Evaluate the remainder of this page
		return EVAL_PAGE;

	}

	/**
	 * Release any acquired resources.
	 */
	public void release() {

		super.release();
		anchor = null;
		href = null;
		linkName = null;
		name = null;
		target = null;
		text = null;

	}

}